var hot = GetQueryString('hot');
var categoryId = GetQueryString('categoryId');
var keywords = GetQueryString('keywords');
var shopNum;
var shopNum2;
var shopNum3;
var page = GetQueryString('page'),
	hotPage = GetQueryString('page'),
	page3 = GetQueryString('page');
page--;
hotPage--;
page3--;
var city = getItem('locationCity');

$(function(){
	if(keywords==""||keywords==undefined||keywords==null){
		if(hot=='hot'){
			$('#name').html('热门商家');
			doHotShop();
		}else{
			var categoryName = decodeURI(decodeURI(window.location.href.split('=')[3]));
			$('#name').html(categoryName);
			doShop();
		}
	}else{
		keywords = decodeURI(decodeURI(window.location.href)).split('=')[1].split('&')[0];
		$('#name').html(keywords);
		dokeywords();
	}
})

//获取该分类下商家列表
function doShop(){
	page++;
	$.ajax({
		url:'/shopC/getCategoryById.json',
		dataType:'json',
		type:'post',
		data:{
			categoryId:categoryId,
			page:page,
			city:city,
		},
		beforeSend:function(){
			if(page==1){
				$('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.newShop'));
			}
		},
		success:function(data){
			if(data.code==1){
				var data = data.datas;
				if(page==1){
					$('.newShop').empty();
					if(data.length==0){
						mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
						$('<div class="kong" style="margin-top: 47px;"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('.newShop'));
					}
				}
				for(var i=0;i<data.length;i++){
					var isNew = data[i].isNewShop;
					if(data[i].isNewShop==0){
						isNew='';
					}else{
						isNew='<div class="newShop-right-sign"><img src="../img/newShop-sign.png" alt="新店"></div>'
					}
					$('<div class="newShop-item mui-clearfix" id="'+data[i].ID+'">'+
						'<div class="newshop-img"><img src="'+data[i].MinImg+'" alt="加载中" class="newShop-logo"></div>'+
						'<div class="newShop-box">'+
						'<p class="newShop-title">'+data[i].ShopName+'</p>'+
						'<p class="newShop-star"><img src="../img/star.png" alt=""></p>'+
						'<p class="newShop-sign">优质好店</p>'+
						'<p class="newShop-line"></p>'+
						'<p class="newShop-user"><img src="../img/newShop-user.png" alt="店主">'+data[i].RealName+'</p>'+
						'<p class="newShop-phone"><img src="../img/newShop-phone.png" alt="电话">'+data[i].Phone+'</p>'+
						'<p class="newShop-location"><img src="../img/newShop-location.png" alt="地址">'+data[i].ShopAddress+'</p>'+
						'</div>'+ isNew+
						'</div>').appendTo($('.newShop'))
				}
				shopNum=data.length;
			}
		}
	})
	return shopNum;
}

//热门商家
function doHotShop(){
	hotPage++;
	$.ajax({
		url:'/shopC/getPopShop.json',
		dataType:'json',
		type:'post',
		data:{
			page:hotPage,
			more:1,
			city:city,
		},
		beforeSend:function(){
			if(hotPage==1){
				$('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.newShop'));
			}
		},
		success:function(data){
			if(data.code==1){
				var data = data.datas;
				if(hotPage==1){
					$('.newShop').empty();
					if(data.length==0){
						mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
						$('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('.newShop'));
					}
				}
				for(var i=0;i<data.length;i++){
					var isNew = data[i].isNewShop;
					if(data[i].isNewShop==0){
						isNew='';
					}else{
						isNew='<div class="newShop-right-sign"><img src="../img/newShop-sign.png" alt="新店"></div>'
					}
					$('<div class="newShop-item mui-clearfix" id="'+data[i].ID+'">'+
						'<div class="newshop-img"><img src="'+data[i].MinImg+'" alt="加载中" class="newShop-logo"></div>'+
						'<div class="newShop-box">'+
						'<p class="newShop-title">'+data[i].ShopName+'</p>'+
						'<p class="newShop-star"><img src="../img/star.png" alt=""></p>'+
						'<p class="newShop-sign">优质好店</p>'+
						'<p class="newShop-line"></p>'+
						'<p class="newShop-user"><img src="../img/newShop-user.png" alt="店主">'+data[i].RealName+'</p>'+
						'<p class="newShop-phone"><img src="../img/newShop-phone.png" alt="电话">'+data[i].Phone+'</p>'+
						'<p class="newShop-location"><img src="../img/newShop-location.png" alt="地址">'+data[i].ShopAddress+'</p>'+
						'</div>'+ isNew+
						'</div>').appendTo($('.newShop'))
				}
				shopNum2 = data.length;
			}
		}
	})
	return shopNum2;
}

function dokeywords(){
	page3++;
	$.ajax({
		url:'/openSearchC/shopSearch.json',
		dataType:'json',
		data:{
			keywords:keywords,
			page:page3,
			city:localStorage.getItem('locationCity'),
		},
		type:'post',
		beforeSend:function(){
			if(page3==1){
				$('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.newShop'));
			}
		},
		success:function(data){
				var data2 = data.datas;
				if(page3==1){
					$('.newShop').empty();
					if(data2.length==0){
						mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
						$('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('.newShop'));
					}
				}
				for(var i=0;i<data2.length;i++){
					$('<div class="newShop-item mui-clearfix" id="'+data2[i].id+'">'+
						'<div class="newshop-img"><img src="'+data2[i].main_img+'" alt="加载中" class="newShop-logo"></div>'+
						'<div class="newShop-box">'+
						'<p class="newShop-title">'+data2[i].name+'</p>'+
						'<p class="newShop-star"><img src="../img/star.png" alt=""></p>'+
						'<p class="newShop-sign">优质好店</p>'+
						'<p class="newShop-line"></p>'+
						'<p class="newShop-user"><img src="../img/newShop-user.png" alt="店主">'+data2[i].real_name+'</p>'+
						'<p class="newShop-phone"><img src="../img/newShop-phone.png" alt="电话">'+data2[i].shop_phone+'</p>'+
						'<p class="newShop-location"><img src="../img/newShop-location.png" alt="地址">'+data2[i].province_name+data2[i].city_name+data2[i].area_name+data2[i].shop_address+'</p>'+
						'</div>'+
						'</div>').appendTo($('.newShop'))
				}
				shopNum3 = data2.length;
		}
	})
	return shopNum3;
}

//上拉加载
mui.init({
	pullRefresh : {
		container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		up : {
			contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
			callback :function(){
				setTimeout(function () {
					var numResult;
					if(keywords==""||keywords==undefined||keywords==null){
						if(hot=='hot'){
							numResult = doHotShop();
						}else{
							numResult = doShop();
						}
					}else{
						keywords = decodeURI(decodeURI(window.location.href)).split('=')[1].split('&')[0];
						numResult = dokeywords();
					}
					if (numResult < 20) {
						mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
					} else {
						mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
					}
				}, 1000);
			} //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
		}
	}
});

//获取20条分类数据
//$(function(){
//	$.ajax({
//		url:'/categoryC/getCategory.json',
//		dataType:'json',
//		data:{
//
//		},
//		type:'post',
//		success:function(data){
//			if(data.code==1){
//				$('#sortBox1').empty();
//				$('#sortBox2').empty();
//				var data =data.datas;
//				for(var i=0;i<=9;i++){
//					$('<a id="'+data[i].ID+'" name="'+data[i].Name+'"><div class="sort"><img src="'+data[i].Img+'"/>'+data[i].Name+'</div></a>').appendTo($('#sortBox1'))
//				}
//				for(var a=10;a<=19;a++){
//					$('<a id="'+data[a].ID+'" name="'+data[a].Name+'"><div class="sort"><img src="'+data[a].Img+'"/>'+data[a].Name+'</div></a>').appendTo($('#sortBox2'))
//				}
//			}
//		}
//	})
//})

//点击分类
//mui('body').on('tap','.sort-box a',function(){
//	var categoryId = $(this).attr('id');
//	var categoryName = $(this).attr('name');
//	window.location.href=encodeURI(encodeURI("shop_list.html?categoryId="+categoryId+"&page=1"+"&categoryName="+categoryName));
//})

//点击跳转商家详情页
mui('body').on('tap','.newShop-item',function(){
	var shopId = $(this).attr('id');
	window.location.href="shop_detail.html?shopId="+shopId;
})

//回到顶部
window.addEventListener('scroll', function(event){
	var offsetTop = event.detail.y;
	if (-offsetTop>300){
		$(".go-top").fadeIn(1000);
	}
	else
	{
		$(".go-top").fadeOut(1000);
	}
	//当点击跳转链接后，回到页面顶部位置
	var goTop = document.getElementById('goTop');
	goTop.addEventListener('tap',function(){
		mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,500);
	})
}, false);